package com.qiwenshare.file.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.stream.Collectors;

public class FileUtils {
    public static void Save(String folder,MultipartFile file){
        if(!folder.endsWith("/")){
            folder += "/";
        }
        File SourceFolder = new File(folder);
        if (!SourceFolder.exists()) {
            SourceFolder.mkdirs();
        }
        try {
            file.transferTo(new File(folder+file.getOriginalFilename()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void Save(String folder,MultipartFile file,String filename){
        if(!folder.endsWith("/")){
            folder += "/";
        }
        File SourceFolder = new File(folder);
        if (!SourceFolder.exists()) {
            SourceFolder.mkdirs();
        }
        try {
            file.transferTo(new File(folder+filename));
            System.out.print("保存文件:"+folder+filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteFile(String folder,String filepath) {
        if(!folder.endsWith("/")){
            folder += "/";
        }
        File file = new File(folder+filepath);
        if(file.isFile()){
            file.delete();
        }
    }


    public static String convertStreamToString(InputStream inputStream){
        String result = new BufferedReader(new InputStreamReader(inputStream))
                .lines().collect(Collectors.joining(System.lineSeparator()));
        return result;
    }

    public static boolean saveObjct2jsonfile(Object jsonData, String filePath) {
        String content = JSON.toJSONString(jsonData, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        // 标记文件生成是否成功
        boolean flag = true;
        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(filePath);
            if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(content);
            write.flush();
            write.close();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean saveJson2jsonfile(String jsonData, String filePath) {
        // 标记文件生成是否成功
        boolean flag = true;
        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(filePath);
            if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(jsonData);
            write.flush();
            write.close();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }
    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            if(jsonFile.exists()){
                Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
                int ch = 0;
                StringBuffer sb = new StringBuffer();
                while ((ch = reader.read()) != -1) {
                    sb.append((char) ch);
                }
                reader.close();
                jsonStr = sb.toString();
            }

            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

